<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="72px"
    >
      <el-form-item label="用户编号" prop="custNo">
        <el-input
          v-model="queryParams.custNo"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="custName">
        <el-input
          v-model="queryParams.custName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区/县" prop="districy">
        <el-input
          v-model="queryParams.districy"
          placeholder="请输入区/县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请人" prop="parentNo">
        <el-input
          v-model="queryParams.parentNo"
          placeholder="请输入邀请人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有效" prop="enabled">
        <el-select
          v-model="queryParams.enabled"
          clearable
          placeholder="请选择"
          @keyup.enter.native="handleQuery"
          style="width: 215px"
        >
          <el-option
            v-for="dict in dict.type.sys_enabled"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['web:cust:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['api:cust:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['web:cust:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['web:cust:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="custList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" min-width="120" show-overflow-tooltip prop="id" /> -->
      <el-table-column
        label="用户编号"
        align="center"
        min-width="100"
        prop="custNo"
      />
      <el-table-column
        label="用户名"
        align="center"
        min-width="100"
        prop="custName"
      />
      <el-table-column
        label="昵称"
        align="center"
        min-width="100"
        prop="nickName"
        show-overflow-tooltip
      />
      <el-table-column
        label="手机号"
        align="center"
        min-width="120"
        prop="mobile"
      />
      <el-table-column
        label="头像"
        align="center"
        prop="avatar"
        min-width="120"
      >
        <template slot-scope="scope">
          <div>
            <el-image
              style="width: 50px; height: 50px; border-radius: 50%"
              :src="scope.row.avatar"
              :preview-src-list="[scope.row.avatar]"
              fit="cover"
              preview-teleported="true"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="会员级别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_cust_type"
            :value="scope.row.custType"
          />
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column
        label="省/市/区/详细地址"
        align="center"
        prop="province"
        width="200"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.province }} {{ scope.row.city }}
          {{ scope.row.districy }} {{ scope.row.address }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="会员积分" align="center" prop="custPoint" min-width="100" sortable /> -->
      <!-- <el-table-column label="账户充值余额" align="center" prop="custBlance" min-width="130" sortable /> -->
      <el-table-column
        label="累计消费"
        align="center"
        prop="cumulative"
        min-width="100"
        sortable
      />
      <el-table-column
        label="待确认佣金"
        align="center"
        prop="outstanding"
        min-width="120"
        sortable
      />
      <el-table-column
        label="钱包总额"
        align="center"
        prop="withdrawableTotal"
        min-width="100"
        sortable
      />
      <el-table-column
        label="钱包余额"
        align="center"
        prop="withdrawable"
        min-width="100"
        sortable
      />
      <el-table-column
        label="邀请人编号"
        align="center"
        prop="parentNo"
        min-width="100"
      />
      <el-table-column
        label="是否有效"
        align="center"
        prop="enabled"
        min-width="80"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_enabled"
            :value="scope.row.enabled"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createDt"
        min-width="160"
        sortable
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['web:cust:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web:cust:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="用户编号" prop="custNo">
          <el-input v-model="form.custNo" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="会员等级" prop="custType">
          <el-select
            v-model="form.custType"
            placeholder="选择用户等级"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_cust_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="邀请人ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入邀请人ID" />
        </el-form-item>
        <el-form-item label="邀请人编号" prop="parentNo">
          <el-input v-model="form.parentNo" placeholder="请输入邀请人编号" />
        </el-form-item>
        <el-form-item label="用户名" prop="custName">
          <el-input v-model="form.custName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="用户性别">
          <el-select
            v-model="form.sex"
            placeholder="请选择性别"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <ImageUpload :limit="1" v-model="form.avatar" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="区/县" prop="districy">
          <el-input v-model="form.districy" placeholder="请输入区/县" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="钱包总额" prop="withdrawableTotal">
          <el-input
            v-model="form.withdrawableTotal"
            placeholder="请输入钱包总额"
          />
        </el-form-item>
        <el-form-item label="钱包余额" prop="withdrawable">
          <el-input v-model="form.withdrawable" placeholder="请输入钱包余额" />
        </el-form-item>
        <el-form-item label="是否有效" prop="enabled">
          <el-select
            v-model="form.enabled"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_enabled"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end"
      >
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCust,
  getCust,
  delCust,
  addCust,
  updateCust,
} from "@/api/djk/memberMgr/allMember";

export default {
  name: "Cust",
  dicts: ["sys_user_sex", "sys_enabled", "sys_cust_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      custList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custNo: null,
        custName: null,
        nickName: null,
        mobile: null,
        sex: null,
        province: null,
        city: null,
        districy: null,
        parentNo: null,
        custType: 3,
        enabled: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        custNo: [
          { required: true, message: "用户编号不能为空", trigger: "blur" },
        ],
        withdrawableTotal: [
          { required: true, message: "钱包总额不能为空", trigger: "blur" },
        ],
        withdrawable: [
          { required: true, message: "钱包余额不能为空", trigger: "blur" },
        ],
        custType: [
          { required: true, message: "会员级别不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      listCust(this.queryParams).then((response) => {
        this.custList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        custNo: null,
        custName: null,
        nickName: null,
        mobile: null,
        password: null,
        email: null,
        openId: null,
        avatar: null,
        sex: null,
        custLable: null,
        birthday: null,
        provinceCode: null,
        province: null,
        cityCode: null,
        city: null,
        districyCode: null,
        districy: null,
        address: null,
        custPoint: null,
        custBlance: null,
        cumulative: null,
        outstanding: null,
        withdrawableTotal: null,
        withdrawable: null,
        parentId: null,
        parentNo: null,
        custType: null,
        enabled: null,
        createDt: null,
        updateDt: null,
        version: null,
        remark: null,
        sysAccount: null,
        occOpenId: null,
        roleType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCust(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCust(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCust(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除会员编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCust(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "web/cust/export",
        {
          ...this.queryParams,
        },
        `cust_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
